<template>
  <div style="margin: 10px 0">
    <div>
      <el-input
          style="width: 200px"
          placeholder="请输入审批编号"
          suffix-icon="el-icon-search"
          v-model="subscibeId"
      ></el-input>
      <el-button class="ml-5" type="primary" @click="load">搜索</el-button>
      <el-button type="warning" @click="reset">重置</el-button>
      <el-button
          type="primary"
          @click="exp"
          class="ml-5"
      >导出 <i class="el-icon-download"></i
      ></el-button>
    </div>

    <div style="margin: 10px 0"></div>
    <el-table
        :data="tableData" border stripe>
      <el-table-column
          fixed
          prop="subscibeId"
          label="审批单号"
          width="80"
      ></el-table-column>
      <el-table-column
          prop="name"
          label="申请购买的设备"
          width="120"
      ></el-table-column>
      <el-table-column
          prop="type"
          label="设备类型"
          width="120"
      ></el-table-column>
      <el-table-column label="设备图片" width="150" class-name="image-column">
        <template slot-scope="scope">
          <img :src="local+'/'+scope.row.picture" alt="Image"/>
        </template>
      </el-table-column>
      <el-table-column prop="price" label="价格" width="120"></el-table-column>
      <el-table-column
          prop="application"
          label="申请人"
          width="120"
      ></el-table-column>
      <el-table-column prop="addtime" label="申请时间" width="300">
        <template slot-scope="{ row }">
          {{ row.addtime | dateFormat }}
        </template>
      </el-table-column>

      <el-table-column label="是否通过" align="center">
        <template slot-scope="scope">
          <el-popconfirm
              class="ml-5"
              confirm-button-text="确定"
              cancel-button-text="等一下"
              icon="el-icon-info"
              icon-color="red"
              title="您确定要同意购买这件资产吗？"
              @confirm="handleEdit(scope.row)"
          >
            <el-button type="success" slot="reference"
            >通过 <i class="el-icon-circle-check"></i
            ></el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <div style="padding: 10px 0">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[5, 10, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import {LocalHost} from "@/utils/tools";

export default {
  data() {
    return {
      tableData: [],
      local: LocalHost,
      total: 0,
      pageNum: 1,
      form: {},
      pageSize: 5,
      showDialog: false,
      selectedRow: {
        Num: "",
        Name: "",
        Type: "",
        Price: "",
        reason: "",
        applicant: "",
        date: "",
      },
      subscibeId: "",
    };
  },
  filters: {
    dateFormat(date) {
      // 将日期转换为JavaScript的Date对象
      const dateObj = new Date(date);
      // 获取年、月和日
      const year = dateObj.getFullYear();
      const month = (dateObj.getMonth() + 1).toString().padStart(2, "0");
      const day = dateObj.getDate().toString().padStart(2, "0");
      // 返回格式化后的日期字符串
      return `${year}-${month}-${day}`;
    },
  },
  created() {
    this.load();
  },
  methods: {
    load() {
      this.request
          .get("/subscribe/page", {
            params: {
              pageNum: this.pageNum,
              pageSize: this.pageSize,
              subscibeId: this.subscibeId,
            },
          })
          .then((res) => {
            if (res.success) {
              this.tableData = res.data.records;
              this.total = res.data.total;
            }
          });
    },
    reset() {
      this.subscibeId = "";
      this.load();
    },
    exp() {
      window.open(LocalHost + "/subscribe/export");
    },
    handleSizeChange(pageSize) {
      this.pageSize = pageSize;
      this.load();
    },
    handleCurrentChange(pageNum) {
      this.pageNum = pageNum;
      this.load();
    },

    handleEdit(row) {
      row.audit = "是";
      this.request.post("/subscribe/audit", row).then((res) => {
        if (res.data) {
          res.addtime = this.addtime;
          this.$message.success("已同意购入申请");
          this.dialogFormVisible = false;
          this.load();
        } else {
          this.$message.error("审批失败");
        }
      });
    },
  },
};
</script>

<style scoped>
.image-column img {
  width: 120px; /* 设置图片宽度 */
  height: 120px; /* 设置图片高度 */
}
</style>